<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./OL_SDK/include-openlayers-local.js"></script>
  <script src="./libs/gaode.js"></script>
  <script src="./utils/createDraw.js"></script>
  <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
  <script src="./utils/setMarkerStyle.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0
    }

    #map {
      width: 100vw;
      height: 100vh;
    }

    #btn {
      width: 200px;
      height: 50px;
      z-index: 100;
      position: fixed;
      left: 50px;
      top: 11px;
      background-color: #6528e0;
      border: none;
      border-radius: 15px;
      color: #fff;
    }

    #btn:hover {
      cursor: pointer;
      background-color: #6528e0a7;
    }
  </style>
</head>

<body>
  <button onclick="activeDraw()" id="btn">激活画布</button>
  <div id="map">
  </div>
  <script>
    var map = new ol.Map({
      target: "map",
      layers: [gaode],
      view: new ol.View({
        projection: 'EPSG:4326',
        center: [114.30, 30.50],
        zoom: 4
      })
    })
    /*
    1、加载geojson数据
    2、设置画笔--获取坐标  box  Polygon
    3、判断geojson要素 是否在Polygon中
    4、在Polygon里面,设置marker
     */
    //1、geojson的加载
    const style = new ol.style.Style({
      // 形状
      fill: new ol.style.Fill({
        color: 'rgba(0,0,0,0.5)'
      }),
      /* 描边 */
      stroke: new ol.style.Stroke({
        color: '#ff2d51',
        width: 2
      }),
      image: new ol.style.Circle({
        radius: 8,
        /* 填充色 */
        fill: new ol.style.Fill({
          color: 'rgba(0,0,0,0.5)'
        }),
        /* 描边 */
        stroke: new ol.style.Stroke({
          color: '#ff2d51',
          width: 0.3
        })
      })
    })
    let marker_style = setMarkerStyle("./images/位置.png")
    var geo_source = new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      url: "./data/query.json"
    })
    var geo_layer = new ol.layer.Vector({
      source: geo_source,
      style
    })
    map.addLayer(geo_layer);
    /* 2、设置画笔 -- 坐标 */
    let draw_source = new ol.source.Vector({

    })
    let draw_layer = new ol.layer.Vector({
      source: draw_source
    })
    map.addLayer(draw_layer);


    /* 3、高亮图层 */
    let high_source = new ol.source.Vector({

    })
    let high_layer = new ol.layer.Vector({
      source: high_source,
      style: setMarkerStyle('./images/位置.png')
    })
    map.addLayer(high_layer)
    function activeDraw() {
      draw_source.clear()
      /* 创建画笔 */
      let draw = createDraw({
        type: "Box",
        source: draw_source
      })
      map.addInteraction(draw);
      draw.on("drawend", evt => {
        high_source.clear()
        var position = evt.feature.getGeometry().getCoordinates()
        /* 3、转化turf --polygon 
        
        判断geo_source 要素是否在polygon */
        let polygon = turf.polygon(position);
        geo_source.forEachFeature(feature => {
          let center = feature.getGeometry().getCoordinates()
          let point = turf.point(center);
          /* 判断 */
          var result = turf.booleanContains(polygon, point);
          if (result) {
            console.log(feature);
            // feature.setStyle(marker_style)
            /* 将获取到的要素设置到注记图层 */
            high_source.addFeature(feature)
          }
        })

        map.removeInteraction(draw)
      })
    }
  </script>
</body>

</html>